<?php
/**
* This is the server side code to create a new box and list existing box types, editing and saving boxes.
* This Request needs one mandatory parameter: "action". It can have the following values:
*<ul>
*<li><b>create</b>: Create a new Box.</li>
*<li><b>list</b>: List all boxes with given parameters.</li>
*<li><b>list_tabs</b>: List all edit_tabs for one Box.</li>
*<li><b>edit_tab</b>: Get the output for an edit tab for a box.</li>
*<li><b>save_tab</b>: Call the SaveTab callback for the given tab of a box.</li>
*<li><b>draw</b>: Draw a box.</li>
*</ul>
* All actions needs to have proper Autherntication.
*@package Box
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*/
class BoxRequest extends CoreRequestJson
{
	private $reload = false;
	/**
	* Internally used for action "save_tab" to notify client-side of the need to reload the page.
	*@param bool $reload True/false: do reload.
	*/
	final public function SetReload($reload = true)
	{
		if ( is_bool( $reload ) )
			$this->reload = $reload;
	}
	public function __construct()
	{
		parent::__construct(true);
	}
	/**
	* Action: "create". Create a new Box.
	*Create a new Box based on some parameters, which are type, tags and title.
	*@param string $type Type of plugin (class name).
	*@param array $tags The new Box tags (tag_cloud).
	*@param string $title The title for the new box.
	*/
	private function Create($type, $tags, $title)
	{
		if ( ($type !== false ) && ( $title !== false ) )
		{
			$atom_id = Box::Create( $type, $title, $tags );
			if ( $atom_id )
			{
				$this->SetOutput("atom_id", $atom_id);
				$this->SetOutput("result", I18N("Added Box!! '$atom_id'"));
			}
			else
				$this->SetError(I18N("Error, title is duplicated!"));
		}
		else
			$this->SetError(I18N("Invalid title ('$title') or type ('$type')"));
	}
	
	private function SaveTab($atom_id, $tab_id)
	{
		if ( ($box = AtomCache::GetAtomData($atom_id)) !== false )
		{
			$box->SaveTab( $tab_id, $this );
			$this->SetOutput("reload",$this->reload );
		}
		else
			$this->SetError("Invalid Atom Data!");
		echo "<button>".I18N("Back to edit...")."</button>";
	}

	private function EditTab($atom_id, $tab_id)
	{
		if ( ($box = AtomCache::GetAtomData($atom_id)) !== false )
		{
			if ( ($tab_data = $box->GetEditTab( $tab_id )) !== false )
			{
				$output = new HtmlTemplate( 'core:box_edit',
										array("atom_id" => $atom_id,
											"tab" => $tab_data), true );
				$this->SetOutput("tab_html", $output->FetchHtml() );
			}
			else
				$this->SetError("Invalid tab_id (edit:$atom_id,tab:$tab_id)!");
		}
		else
			$this->SetError("Invalid Atom Data (edit:$atom_id)!");
	}

	private function ListTabs($atom_id)
	{
		if ( ($box = AtomCache::GetAtomData($atom_id)) !== false )
			$this->SetOutput("tabs", $box->ListEditTabs() );
		else
			$this->SetError("Invalid Atom Data (list:$atom_id)!");
	}

	private function DrawBox( $atom_id )
	{
		Box::Draw( $atom_id );
	}

	public function Process()
	{
		$action = Core::PostVar("action", false, "string");
		if ( ($action === "create") )
		{
			$this->Create(Core::PostVar("type", false, "string"),
							Core::PostVar("tags", array(), "sequence"),
							Core::PostVar("title", false, "string"));
		}
		else if ( ($action === "list_tabs") )
		{
			$this->ListTabs(Core::PostVar("atom_id", false, "string"));
		}
		else if ( ($action === "edit_tab") )
		{
			$this->EditTab(Core::PostVar("atom_id", false, "string"),
							Core::PostVar("tab_id", 0, "int"));
		}
		else if ( ($action === "save_tab") )
		{
			$this->SaveTab(Core::PostVar("atom_id", false, "string"),
							Core::PostVar("tab_id", 0, "int"));
		}
		else if ( $action === "draw" )
		{
			$this->DrawBox( Core::PostVar("atom_id", false, "string") );
		}
	}
}
?>